package com.jiangyg.mall.product.controller;

import com.jiangyg.mall.core.support.restful.Result;
import com.jiangyg.mall.product.dto.AttrAttrgroupDTO;
import com.jiangyg.mall.product.dto.AttrgroupDTO;
import com.jiangyg.mall.product.vo.AttrVO;
import com.jiangyg.mall.product.vo.AttrgroupVO;
import com.jiangyg.mall.product.service.AttrgroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 类描述：属性分组控制器
 *
 * @author jiangyg
 * @version email@jiangyg.com
 * @date 2021-10-30 07:38:50
 */
@RestController
@RequestMapping("/product/attrgroup")
public class AttrgroupController {

    /**
     * 属性分组接口
     */
    private final AttrgroupService attrgroupService;

    @Autowired
    public AttrgroupController(AttrgroupService attrgroupService) {
        this.attrgroupService = attrgroupService;
    }

    /**
     * 功能描述：属性分组查询
     *
     * @param request    请求
     * @param categoryId 分组ID
     * @return 返回分页属性分组
     */
    @ResponseBody
    @PostMapping("/list")
    public Result<List<AttrgroupVO>> list(HttpServletRequest request, @RequestParam("categoryId") String categoryId) throws Exception {
        return Result.ok(attrgroupService.selectList(categoryId));
    }

    /**
     * 功能描述：属性分组查询，附带分组下属性
     *
     * @param request    请求
     * @param categoryId 分组ID
     * @return 返回分页属性分组
     */
    @ResponseBody
    @PostMapping("/list-attach-attr")
    public Result<List<AttrgroupVO>> listAttachAttr(HttpServletRequest request, @RequestParam("categoryId") Long categoryId) throws Exception {
        return Result.ok(attrgroupService.listAttachAttr(categoryId));
    }

    /**
     * 功能描述：保存或者修改属性分组
     *
     * @param request 请求
     * @param dto     属性分组
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/save")
    public Result<Object> save(HttpServletRequest request, @RequestBody @Validated AttrgroupDTO dto) throws Exception {
        return Result.saveOrUpdate(attrgroupService.saveOrUpdate(dto));
    }

    /**
     * 功能描述：根据ID获取属性分组
     *
     * @param request     请求
     * @param attrGroupId 属性分组ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/info")
    public Result<AttrgroupVO> info(HttpServletRequest request, @RequestParam(name = "attrgroupId") Long attrGroupId) throws Exception {
        return Result.ok(attrgroupService.selectById(attrGroupId));
    }

    /**
     * 功能描述：删除属性分组
     *
     * @param request      请求
     * @param attrgroupIds 批量删除的属性分组ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/delete")
    public Result<Object> delete(HttpServletRequest request, @RequestBody List<Long> attrgroupIds) {
        return Result.delete(attrgroupService.delete(attrgroupIds));
    }

    /**
     * 功能描述：属性分组和属性关联分页查询
     *
     * @param request     请求
     * @param attrgroupId 分组ID
     * @return 返回分页属性分组
     */
    @ResponseBody
    @PostMapping("/relation/list")
    public Result<List<AttrVO>> list(HttpServletRequest request, @RequestParam("attrgroupId") Long attrgroupId) throws Exception {
        return Result.ok(attrgroupService.listRelation(attrgroupId));
    }

    /**
     * 功能描述：分类下没有关联的属性
     *
     * @param categoryId  分类ID
     * @return 返回分页属性分组
     */
    @ResponseBody
    @PostMapping("/notrelation/list")
    public Result<List<AttrVO>> listNotRelation(@RequestParam("categoryId") Long categoryId) throws Exception {
        return Result.ok(attrgroupService.listNotRelation(categoryId));
    }

    /**
     * 功能描述：保存属性分组和属性关联
     *
     * @param request 请求
     * @param dto     参数
     * @return 返回分页属性分组
     */
    @ResponseBody
    @PostMapping("/relation/save")
    public Result<Object> saveRelation(HttpServletRequest request, @RequestBody AttrAttrgroupDTO dto) throws Exception {
        return Result.saveOrUpdate(attrgroupService.saveRelation(dto));
    }

    /**
     * 功能描述：删除属性分组和属性关联
     *
     * @param request 请求
     * @param dto     参数
     * @return 返回分页属性分组
     */
    @ResponseBody
    @PostMapping("/relation/delete")
    public Result<Object> deleteRelation(HttpServletRequest request, @RequestBody AttrAttrgroupDTO dto) {
        return Result.delete(attrgroupService.deleteRelation(dto));
    }

}
